package com.hdax.dm.feign.configuration;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//@Component
@Slf4j
public class FeignWithTokenConfiguration implements RequestInterceptor {

    private static final String TOKEN_HEADER_NAME = "token";
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader("token");
        if(token == null){
            log.error("请求头信息中不包含token信息");
            return;
        }
        template.header(TOKEN_HEADER_NAME,token);
    }
}
